package com.example.tys.baseapplication.net_tcp.frame;


import com.example.tys.baseapplication.net_tcp.frame.packet.Packet;

import java.net.InetSocketAddress;

/**
 *
 * @author tys
 * @date 2019/5/3
 */

public class PacketRequest extends Packet {

    private byte[] head;
    private byte[] data;

    public PacketRequest(String uniqueId) {
        super(uniqueId);
    }

    public PacketRequest(String uniqueId, InetSocketAddress targetAddress) {
        super(uniqueId, targetAddress);
    }

    public static PacketRequest createdTcpRequest() {
        PacketRequest packetRequest = new PacketRequest("12345");
        packetRequest.head = createdHead();
        packetRequest.data = createdData();
        return packetRequest;
    }


    public static PacketRequest createdUdpRequest(String ip, int port) {
        PacketRequest packetRequest = new PacketRequest("12345",
                new InetSocketAddress(ip, port));
        packetRequest.head = createdHead();
        packetRequest.data = createdData();
        return packetRequest;
    }

    private static byte[] createdData() {
        byte[] data = new byte[812];
        data[0] = 0;
        data[1] = 0;
        data[2] = 0;
        data[3] = 2;
        return data;
    }


    static byte[] createdHead() {
        byte[] head = new byte[652];
        //START MASK
        head[0] = (byte) 0xBB;
        head[1] = (byte) 0xAA;
        //分包数
        head[2] = 1;
        //当前分包编号
        head[3] = 0;
        //该信息发布者账号  64
        byte[] bytes = "IFARM_PHONE".getBytes();
        System.arraycopy(bytes, 0, head, 4, bytes.length);
        //信息发布者密码  64
        bytes = "SumPhone2018".getBytes();
        System.arraycopy(bytes, 0, head, 68, bytes.length);
        //信息编号  64
        bytes = "testid-a".getBytes();
        System.arraycopy(bytes, 0, head, 132, bytes.length);
        //信息名称  64
        bytes = "150-APPLY".getBytes();
        System.arraycopy(bytes, 0, head, 196, bytes.length);
        //信息功能码
        head[260] = (byte) 0x96;
        //信息类型
        head[261] = 0;
        //信息等级
        head[262] = 6;
        //发布对象个数
        head[263] = 1;

        //信息源唯一编号(WebServer) 64
        bytes = "testSimulator".getBytes();
        System.arraycopy(bytes, 0, head, 264, bytes.length);
        //发布对象唯一编号 [5][64]
        bytes = "IFarmServer".getBytes();
        System.arraycopy(bytes, 0, head, 328, bytes.length);
//        System.arraycopy(bytes, 0, head, 392, bytes.length);
//        System.arraycopy(bytes, 0, head, 456, bytes.length);
//        System.arraycopy(bytes, 0, head, 520, bytes.length);
//        System.arraycopy(bytes, 0, head, 584, bytes.length); //584 + 64 = 648
        //实际数据内容大小，总大小，不是每帧的大小
        head[648] = 0x04; //低位
        head[649] = 0x00; //高位
        //END MASK
        head[650] = 0x66;
        head[651] = 0x55;
        return head;
    }


    public byte[] getHead() {
        return head;
    }

    public void setHead(byte[] head) {
        this.head = head;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
